String.prototype.trim=function(){
	return this.replace(/^\s*|\s*$/g, "");
};
$(document).ready(function(){
	displayLang = $("#display-lang").val();
	$("#pwd").val("");
	$("#repwd").val("");
	$("input[name=prefer-langs][value=zh]").attr("checked",false);
	$(".ftext").focus(function(){
		$(this).css("border-color","#256EB1");
		$(this).parent().find(".error-msg").hide();
		$(this).parent().find(".tips").show();
	});
	$("input[name=prefer-langs]").change(function(){
		$("#p-lang").parent().find(".error-msg").hide();
	});
	$("#email").blur(function(){
		emailCheck(this);
	});	
	$("#uid").blur(function(){
		nameCheck(this);
	});	
	$("#pwd").blur(function(){
		pwdCheck(this);
	});
	$("#repwd").blur(function(){
		repwdCheck(this);
	});
});	

function blurState(e){
	$(e).css("border-color","#BDC7D8");
	$(e).parent().find(".tips").hide();
}
function emailCheck(e){
	var flag = true;
	blurState(e);
	var val =$(e).val();
	if(val==""){
		var msg =$(e).parent().find(".error-msg");
		var textEn = "Please enter an email";
		var textZhSimple ="请输入邮箱";
		if(displayLang=="en")
			msg.text(textEn);
		else if(displayLang=="zh")
			msg.text(textZhSimple);
		msg.show();
		$(e).css("border-color","red");
		flag = false;
	}else{
		var emailReg=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
		if(!emailReg.test(val)){
			var msg =$(e).parent().find(".error-msg");
			var textEn = "Wrong format of email";
			var textZhSimple ="邮箱格式错误";
			if(displayLang=="en")
				msg.text(textEn);
			else if(displayLang=="zh")
				msg.text(textZhSimple);
			msg.show();
			$(e).css("border-color","red");
			flag = false;
		}else{
			flag = emailExistTest(val,e);
		}
		
	}
	if(!flag)
		return false;
	return true;
}
function emailExistTest(val,e){
	var flag  = true;;
	 var data={};
	 data["action"]="emailExistTest";
	 data["value"] = val;
     data =  JSON.stringify(data);
	 data=("register="+encodeURIComponent(data));
	$.ajax({
	  url:"loginValidate",
	  async:false,
	  data:data,
	  dataType:"text",
	  type:"post" ,
	  success:function(text){
		  if(text=="exist"){
		     flag = false;
			 var msg =$(e).parent().find(".error-msg");
			 var textEn = "This email has existed. Please try another one.";
			 var textZhSimple ="该邮箱已经存在。请使用其它邮箱";
			 if(displayLang=="en")
				msg.text(textEn);
			 else if(displayLang=="zh")
				msg.text(textZhSimple);
			 msg.show();
			 $(e).css("border-color","red");		
		  }
	  }
	});
	return flag;
}
function nameCheck(e){
	var flag =true;
	blurState(e);
	var val =$(e).val();
	if(val==""){
		var msg =$(e).parent().find(".error-msg");
		var textEn = "Please enter a name";
		var textZhSimple ="请输入用户名";
		if(displayLang=="en")
			msg.text(textEn);
		else if(displayLang=="zh")
			msg.text(textZhSimple);
		msg.show();
		$(e).css("border-color","red");
		flag = false;
	}else{
		var nameReg=/^[A-Za-z0-9_]{3,15}$/;
		if(!nameReg.test(val)){
			var msg =$(e).parent().find(".error-msg");
			var textEn = "Wrong format of name";
			var textZhSimple ="用户名格式错误";
			if(displayLang=="en")
				msg.text(textEn);
			else if(displayLang=="zh")
				msg.text(textZhSimple);
			msg.show();
			$(e).css("border-color","red");
			flag = false;
		}else{
			flag = nameExistTest(val,e);
		}
	}
	if(!flag)
		return false;
	return true;
}
function nameExistTest(val,e){
	var flag = true;
	 var data={};
	 data["action"]="nameExistTest";
	 data["value"] = val;
     data =  JSON.stringify(data);
	 data=("register="+encodeURIComponent(data));
	 $.ajax({
	  url:"loginValidate",
	  async:false,
	  data:data,
	  dataType:"text",
	  type:"post" ,
	  success:function(text){
		  if(text=="exist"){
		     flag = false;
			 var msg =$(e).parent().find(".error-msg");
			 var textEn = "This name has existed. Please try another one.";
			 var textZhSimple ="该用户名已经存在。请使用其它用户名";
			 if(displayLang=="en")
				msg.text(textEn);
			 else if(displayLang=="zh")
				msg.text(textZhSimple);
			 msg.show();
			 $(e).css("border-color","red");
		  }
	  }
	});
	 return flag;
}
function pwdCheck(e){
	var flag =true;;
	blurState(e);
	var val =$(e).val();
	if(val==""){
		var msg =$(e).parent().find(".error-msg");
		var textEn = "Please enter a password";
		var textZhSimple ="请输入密码";
		if(displayLang=="en")
			msg.text(textEn);
		else if(displayLang=="zh")
			msg.text(textZhSimple);
		msg.show();
		$(e).css("border-color","red");
		flag = false;
	}else{
		var pwdReg=/^[A-Za-z0-9]{6,14}$/;
		if(!pwdReg.test(val)){
			var msg =$(e).parent().find(".error-msg");
			var textEn = "Wrong format of password";
			var textZhSimple ="密码格式错误";
			if(displayLang=="en")
				msg.text(textEn);
			else if(displayLang=="zh")
				msg.text(textZhSimple);
			msg.show();
			$(e).css("border-color","red");
			flag = false;
		}
	}
	if(!flag)
		return false;
	return true;
}
function repwdCheck(e){
	var flag =true;
	blurState(e);
	var val =$(e).val();
	if(val==""){
		var msg =$(e).parent().find(".error-msg");
		var textEn = "Please re-type the password";
		var textZhSimple ="请输入确认密码";
		if(displayLang=="en")
			msg.text(textEn);
		else if(displayLang=="zh")
			msg.text(textZhSimple);
		msg.show();
		$(e).css("border-color","red");
		flag = false;
	}else{
		if(val!=$("#pwd").val()){
			var msg =$(e).parent().find(".error-msg");
			var textEn = "Passwords not identical";
			var textZhSimple ="两次密码不一致";
			if(displayLang=="en")
				msg.text(textEn);
			else if(displayLang=="zh")
				msg.text(textZhSimple);
			msg.show();
			$(e).css("border-color","red");
			flag = false;
		}
	}	
	if(!flag)
		return false;
	return true;
}
function langCheck(){
	var flag = false;
	$("input[name=prefer-langs]").each(function(index){
		if($(this).attr("checked")!="undefined"&&$(this).attr("checked")=="checked"){
			flag = true;
			return false;
		}
	});	
	if(!flag){
		var msg = $("#p-lang").parent().find(".error-msg");
		var textEn = "Please select languages";
		var textZhSimple ="请选择语言";
		if(displayLang=="en")
			msg.text(textEn);
		else if(displayLang=="zh")
			msg.text(textZhSimple);
		msg.show();
	}
	return flag;
}
function validateCheck(){
    if(emailCheck($("#email"))&&nameCheck($("#uid"))&&
    		pwdCheck($("#pwd"))&&repwdCheck($("#repwd"))&&langCheck())
		$("#registerForm").submit();	
	return false;	
}